﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.Services;
using DDSC.OracleADO;
using System.Collections;
using System.Text;
using System.Data;

public partial class krj_document_category :  BasePermissionPage
{
    //維護動作
    public string Mode { get; set; }
    
    [WebMethod]
    public static Object qtyCATEGORY(string DEPT_ID, PageInfo p_PageInfo)
    {
        dbAccess m_dba = new dbAccess();
        Object results = new Object();
        StringBuilder m_sb = new StringBuilder();

        try
        {
            DataSet ds = null;
            ArrayList m_arrParam = new ArrayList();
            m_sb.Clear();
            m_sb.Append(" select distinct A.dept_id || ' ' || B.dept_name dept_id, A.document_category category_id, A.document_category_name ");
            m_sb.Append(" FROM KRJ_CATEGORY_DOCUMENT A left join mis_dept_ap B on A.DEPT_ID = B.DEPT_ID  ");
            m_sb.Append(" Where 1=1 ");
            if (DEPT_ID != "")
            {
                m_sb.Append(" and A.dept_id = :DEPT_ID ");
                m_arrParam.Add(m_dba.GetOracleParam(":DEPT_ID", DEPT_ID));
            }

            results = ADOGridHandler(m_sb, m_arrParam, p_PageInfo);
        }
        catch (Exception ex)
        {
            BasePermissionPage BP = new BasePermissionPage();
            results = BP.BP_Message;
            BP.ERROR_DBSAVE("krj_document_category.aspx.cs", "qtyCATEGORY", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return results;
    }
    [WebMethod]
    public static Object Getdocument_category(string DEPT_ID, string CATEGORY_ID, string CATEGORY_NAME, PageInfo p_PageInfo)
    {
        dbAccess m_dba = new dbAccess();
        Object results = new Object();
        StringBuilder m_sb = new StringBuilder();
               
        try
        {
            DataSet ds = null;
            ArrayList m_arrParam = new ArrayList();
            m_sb.Clear();
            m_sb.Append(" select distinct  A.dept_id || ' ' ||B.DEPT_NAME dept_id, document_category, document_category_name ,A.dept_id DEPT_ID_H ");
            m_sb.Append(" FROM KRJ_CATEGORY_DOCUMENT A left join MIS_DEPT_AP B on A.DEPT_ID = B.DEPT_ID  ");
            m_sb.Append(" Where 1=1 ");
            if (DEPT_ID != "")
            {
                m_sb.Append(" and A.dept_id = :DEPT_ID ");
                m_arrParam.Add(m_dba.GetOracleParam(":DEPT_ID", DEPT_ID));
            }
            if (CATEGORY_ID != "")
            {
                m_sb.Append(" and document_category = :CATEGORY_ID ");
                m_arrParam.Add(m_dba.GetOracleParam(":CATEGORY_ID", CATEGORY_ID));
            }
            if (CATEGORY_NAME != "")
            {
                m_sb.Append(" and document_category_name like :CATEGORY_NAME ");
                m_arrParam.Add(m_dba.GetOracleParam(":CATEGORY_NAME", "%" + CATEGORY_NAME + "%"));
            }
            
            results = ADOGridHandler(m_sb, m_arrParam, p_PageInfo);
            //20130628**********************************************
            //記錄最後查詢的條件
            krj_document_category sa = new krj_document_category();
            sa.savecondition(DEPT_ID, CATEGORY_ID, CATEGORY_NAME);
            //20130628**********************************************
        }
        catch (Exception ex)
        {
            //results = ex.Message;
            BasePermissionPage BP = new BasePermissionPage();
            results = BP.BP_Message;
            BP.ERROR_DBSAVE("krj_document_category.aspx.cs", "Getdocument_category", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return (results);

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            //Modify By Ben 20130621 修改傳遞方式=======
            //紀錄Grid裡面的Key用於傳遞到維護頁 Session 為程式名稱
            if (Session["krj_document_category"] != null)
            {
                SetKey("krj_document_category");
            }
            //紀錄頁次
            var m_PageIndex = "1";

            DataSet ds = null;
            ds = DBINT();
            if (ds != null && ds.Tables[0].Rows[0]["ReturnCode"] == "000")
            {
                //DropDownList資料bind入
                ddldept.DataTextField = "DEPT_NAME";
                ddldept.DataValueField = "DEPT_ID";
                ddldept.DataSource = ds.Tables["DEPT"];
                ddldept.DataBind();
            }
            

            //判斷如果是維護頁導回來就填回查詢條件
            if (Page.PreviousPage != null)
            {
                //處理查詢條件
                if (g_ConditionKey != null)
                {
                    ddldept.SelectedValue = g_ConditionKey["DEPT_ID"].ToString();
                    txtCategory.Text = g_ConditionKey["CATEGORY_ID"].ToString();
                    txtCategory_NAME.Text = g_ConditionKey["CATEGORY_NAME"].ToString();

                    m_PageIndex = g_ConditionKey["PageIndex"].ToString();
                }
                //執行搜尋功能
                ClientScript.RegisterClientScriptBlock(this.GetType(), "btnSearch_Click", "<script language='javascript'>$(document).ready(function () { btnSearch_Click('" + m_PageIndex + "'); }) </script>");
            }


        }
        catch (Exception ex)
        {
            //Response.Write(e.Message);
            //lbl_message.Text = ex.Message;
            BasePermissionPage BP = new BasePermissionPage();
            lbl_message.Text = BP.BP_Message;
            BP.ERROR_DBSAVE("krj_document_category.aspx.cs", "Page_Load", ex.Message, "", "c#", "0", ex.StackTrace);

        }
    }
    protected DataSet DBINT()
    {
        DataSet ds = null;
        try
        {
            dbAccess m_dba = new dbAccess();
            StringBuilder m_sb = new StringBuilder();
            ArrayList m_arrParam = new ArrayList();
            m_sb.Append(" select A.bi4101 DEPT_ID ,A.bi4101 || '-' || B.DEPT_NAME DEPT_NAME ");
            m_sb.Append(" from as400_bif41 A left join MIS_DEPT_AP  B on A.BI4101 = B.DEPT_ID ");
            m_sb.Append(" where A.bi4101 < '1000' ");
            m_sb.Append(" order by A.bi4101 ");
            ds = m_dba.runExecuteQuery("DEPT", m_sb.ToString(), m_arrParam);
            

        }
        catch (Exception ex)
        {
            //lbl_message.Text = ex.Message;
            lbl_message.Text = BP_Message;
            ERROR_DBSAVE("krj_document_category.aspx.cs", "DBINT", ex.Message, "", "c#", "0", ex.StackTrace);
        }
        return ds;
    }
    protected void btn_exist_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }
    protected void btn_insert_Click(object sender, EventArgs e)
    {
        Mode = "Insert";
        toPage();
    }
    protected void btn_edit_Click(object sender, EventArgs e)
    {
        Mode = "Update";
        toPage();
    }
    protected void btn_delete_Click(object sender, EventArgs e)
    {
        Mode = "Delete";
        toPage();
    }
    protected void btn_VIEW_Click(object sender, EventArgs e)
    {
        Mode = "View";
        toPage();

    }
    protected void toPage()
    {
        Server.Transfer("~/Management/Forms/krj_document_category_detail.aspx", false);

    }
    protected void savecondition(string DEPT_ID, string CATEGORY_ID, string CATEGORY_NAME)
    {
        try
        {
            DropDownList ddl = new DropDownList();
            TextBox tx = new TextBox();
            
            var controlItem = new List<ControlItem>() 
            {
              new ControlItem("ddldept", DEPT_ID, ddl.GetType()),
              new ControlItem("txtCategory", CATEGORY_ID, tx.GetType()),
              new ControlItem("txtCategory_NAME", CATEGORY_NAME, tx.GetType())      
            };
            ConditionRecordUtil.RecordSearchConditionData(controlItem);
        }
        catch (Exception ex)
        {
            ERROR_DBSAVE("krj_document_category.aspx.cs", "savecondition", ex.Message, "", "c#", "0", ex.StackTrace);
        }
    }
}